package com.xiangban.yanglaodajie_platform.aop;

import com.xiangban.yanglaodajie_platform.entity.res.ApiResultCode;
import com.xiangban.yanglaodajie_platform.exception.BizException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

import static com.xiangban.yanglaodajie_platform.aop.AnnotationParse.*;

/**
 * @author : manke
 * @Description
 * @Date: 2020/10/31 11:09
 */

@Aspect
@Component
public class ControllerAspect {
    private static final Logger logger = LoggerFactory.getLogger(ControllerAspect.class);
    @Autowired
    private AnnotationParse annotationParse;

    @Pointcut("execution(public * com.xiangban.yanglaodajie_platform.controller.*.*(..))")
    private void Permission() {

    }
    @Around("Permission()")
    public Object isAccessMethod(ProceedingJoinPoint proceedingJoinPoint)
            throws Throwable {
        //获取访问目标方法
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method method = methodSignature.getMethod();
        //得到方法的访问权限
        Integer flag = annotationParse.Permission(method);
        //如果该方法上没有权限注解，直接调用目标方法
        if (flag == flag_no_token) {
            logger.info("==========未携带token");
            throw new BizException(ApiResultCode.NOT_TOKEN);
        }
        if (flag == flag_failed) {
            logger.info("==========token验证失败");
            throw new BizException(ApiResultCode.FAILED_TOKEN);
        }
        return proceedingJoinPoint.proceed();

    }
}
